package com.bdtop.dbl.utils;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;

public class CacheUtil {
    
    /**
     * 
     * @Title:getDiskCacheDir
     * @Description:  �õ�����Ŀ¼ 
     * @param @param context 
     * @param @param uniqueName �������
     * @param @return    
     * @return File
     * @throws
     */
    public static File getDiskCacheDir(Context context, String uniqueName) {  
        String cachePath;  
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())  
                || !Environment.isExternalStorageRemovable()) {  
            if(null!=context.getExternalCacheDir()){
                cachePath = context.getExternalCacheDir().getPath();
            }else{
                cachePath = context.getCacheDir().getPath(); 
            }
        } else {  
            cachePath = context.getCacheDir().getPath();  
        }  
        return new File(cachePath + File.separator + uniqueName);  
    }
    
    public static File getDiskHeadFileDir(Context context, String uniqueName) {  
        String cachePath;  
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())  
                || !Environment.isExternalStorageRemovable()) {  
            if(null!=context.getExternalCacheDir()){
                cachePath = context.getExternalFilesDir("head").getPath();
            }else{
                cachePath = context.getFilesDir().getPath(); 
            }
        } else {  
            cachePath = context.getCacheDir().getPath();  
        }  
        return new File(cachePath + File.separator + uniqueName);  
    }

    /**
     * 
     * @Title: getAppVersion 
     * @Description: �õ��汾��
     * @param @param context
     * @param @return    
     * @return int
     * @throws
     */
    public static int getAppVersion(Context context) {  
        try {  
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);  
            return info.versionCode;  
        } catch (NameNotFoundException e) {  
            e.printStackTrace();  
        }  
        return 1;  
    }  

    /**
     * 
     * @Title: open 
     * @Description: �õ�DiskLruCacheʵ��
     * @param @param context
     * @param @param dataType
     * @param @return    
     * @return DiskLruCache
     * @throws
     */
    public static DiskLruCache open(Context context,String fileName){
        DiskLruCache mDiskLruCache = null;  
        try {
            File cacheDir = getDiskCacheDir(context, fileName);
            System.out.println(cacheDir.getAbsolutePath());
            if (!cacheDir.exists()) {  
                cacheDir.mkdirs();  
            }  
            mDiskLruCache = DiskLruCache.open(cacheDir, getAppVersion(context), 1, 10 * 1024 * 1024);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return mDiskLruCache;
    }
    
}
